/***************************************************************************
 *   Copyright (C) 2009 by MJ   *
 *   hanfman_cool@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//screen.cpp - is a part from steamserverutil by MJ
//steamserverutil.googlecode.com

//screen.cpp handles the screen feature of steamserverutil

#include <iostream> //for cout and cin
#include <fstream> //for ifstream and ofstream
#include <stdlib.h> //for system()	
#include <string.h> //for strcpy, strcat and strcmp

#include "steamserverutil.h"


using namespace std;

int server_screen[25]; //same procedure as in is_installed()
int n = 1;

void started_serversWithScreen(int i)
{
	n = 1;

	
	switch( i )
	{
		case 0:
			cout << n <<".\tCounter Strike Source Server is running\n";
			server_screen[n] = 0;
			n++;
			break;
		case 1:
			cout << n <<".\tDay of Defeat is running\n";
			server_screen[n] = 1;
			n++;
			break;
		case 2:
			cout << n <<".\tGarrysmod Server is running\n";
			server_screen[n] = 2;
			n++;
			break;
		case 3:
			cout << n <<".\tHalf Life 2 Death Match Server is running\n";
			server_screen[n] = 3;
			n++;
			break;
		case 4:
			cout << n <<".\tTeam Fortress Server is running\n";
			server_screen[n] = 4;
			n++;
			break;
		case 5:
			cout << n <<".\tOpposing Force Server is running\n";
			server_screen[n] = 5;
			n++;
			break;
		case 6:
			cout << n <<".\tCounter Strike Condition Zero Server is running\n";
			server_screen[n] = 6;
			n++;
			break;
		case 7:
			cout << n <<".\tDeath Match Classic Server is running\n";
			server_screen[n] = 7;
			n++;
			break;
		case 8:
			cout << n <<".\tDay of Defeat Server is running\n";
			server_screen[n] = 8;
			n++;
			break;
		case 9:
			cout << n <<".\tCounter Strike Source Server is running\n";
			server_screen[n] = 9;
			n++;
			break;
		case 10:
			cout << n <<".\tRicochet Server is running\n";
			server_screen[n] = 10;
			n++;
			break;
		case 11:
			cout << n <<".\tTeam Fortress Classic Server is running\n";
			server_screen[n] = 11;
			n++;
			break;
		case 12:
			cout << n <<".\tDark Messiah is running\n";
			server_screen[n] = 12;
			n++;
			break;
		case 13:
			cout << n <<".\tChivalry Server is running\n";
			server_screen[n] = 13;
			n++;
			break;
		case 14:
			cout << n <<".\tD.I.P.R.I.P Server is running\n";
			server_screen[n] = 14;
			n++;
			break;
		case 15:
			cout << n <<".\tIncurgency Server is running\n";
			server_screen[n] = 15;
			n++;
			break;
		case 16:
			cout << n <<".\tSynergy Server is running\n";
			server_screen[n] = 16;
			n++;
			break;
		case 17:
			cout << n <<".\tZombie Panic Mod Server is running\n";
			server_screen[n] = 17;
			n++;
			break;
		case 18:
			cout << n <<".\tEnternal Silence (Mod) Server is running\n";
			server_screen[n] = 18;
			n++;
			break;
		case 19:
			cout << n <<".\tLeft 4 Dead Server is running\n";
			server_screen[n] = 19;
			n++;
			break;
		default: never_happen();
			cout << "\n\nUnknown Error occured!\n\n";
			break;
	}
}

void screen_test()
{
	server_screen[0] = -1; //to check if any server is started
	
	bool exit = false;
	
	while(!exit)
	{
	
		cout << "\n\nCurrently running servers with \"screen\": \n\n";
	
		char command[300];
	
		strcpy(command, "screen -ls | grep ");
	
	
		char array[20][15]; //first saving the process name in screen and the the id
		
		strcpy(array[0], "css");
		strcpy(array[1], "dods");
		strcpy(array[2], "garrysmod");
		strcpy(array[3], "hl2mp");
		strcpy(array[4], "tf");
		strcpy(array[5], "cstrike");	
		strcpy(array[6], "cscz");
		strcpy(array[7], "dmc");
		strcpy(array[8], "dod");
		strcpy(array[9], "gearbox");
		strcpy(array[10], "ricochet");
		strcpy(array[11], "tfc");
		strcpy(array[12], "darkmessiah");
		strcpy(array[13], "chivalry");
		strcpy(array[14], "diprip");
		strcpy(array[15], "insurgency");
		strcpy(array[16], "synergy");
		strcpy(array[17], "zps");
		strcpy(array[18], "esmod");
		strcpy(array[19], "l4d");
		
		char cur_id[17][32];
	
		for (int i = 0; i < 20; i++)
		{
			strcpy(&command[18], array[i]);
			strcat(command, " > temp.txt");
		
			system(command);
		
			//reading temp.txt
		
			ifstream in;
			in.open("temp.txt");
		
			if(in.is_open())
			{
			
				while (!in.eof())
				{
					char temp[50];
					in.getline(temp, 50);
				
					switch(temp[0])
					{
					
						case 0x09: //means a tab '/t'
							for(int m=0; m<(32-4); m++)
							{
								if(temp[(m+1)] == '.')
								{
									cur_id[i][m] = 0;
									break;
								}
								else
									cur_id[i][m] = temp[(m+1)];
								
							}
							
							started_serversWithScreen(i);
				
						
							break;
					}
				}
				
				in.close();
				system("rm -rf temp.txt");
			}
			else
			{
				in.close();
				cout << "\n\nAn error occured while reading temp.txt, maybe you have no permission to write or screen is not installed!\n";
				never_happen();
			
				return;
			}
			
			
			
		}
		
		if(server_screen[0] == -1)
		{
			cout << "\n\nIt seems like that there is now server started with screen!\n\n";
			return;
		}
		
		
		cout << "\n\n1. Kill a server\n";
		cout << "2. Exit\n";
		cout << " : ";
		
		unsigned int choice;
		cin >> choice;
		
		switch (choice)
		{
			case 1: cout << "\n\nWhich server should be halted? ";
			cin >> choice;
			if(choice > 20)
				cout << "Wrong choice!";
			
			else
			{
				char temp[10];
				strcpy(temp, "kill ");
				strcat(temp, cur_id[server_screen[choice]]);//(choice-1)]);
					
				cout << temp;
					
				cout << "\n\nKilling the server...";
				cout << flush;
				system(temp);
			}
			break;
			case 2: exit = true;
			break;
			default: cout << "\n\nTry again;)\n";
			break;
			
		}
		
		
	
	}
	
}